package org.scirp.ijmis.dao.review.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.scirp.ijmis.dao.base.impl.BaseDAOImpl;
import org.scirp.ijmis.dao.review.IReviewPaperDAO;
import org.scirp.ijmis.po.review.ReviewPaper;
import org.scirp.ijmis.util.Pagination;

public class ReviewPaperDAOImpl extends BaseDAOImpl<ReviewPaper> implements IReviewPaperDAO {

	private static final String className = ReviewPaperDAOImpl.class.toString();

	public ReviewPaper selectById(Long id) {
		return (ReviewPaper) super.selectById(id, className + "-->selectById");
	}

	public boolean updateReviewPaper(ReviewPaper reviewPaper) {
		return super.updateObject(reviewPaper, className + "-->updateReviewPaper");
	}

	public ReviewPaper selectByPaperIdAndPersonId(Long paperId, Long personId) {
		String hql = "from ReviewPaper where paper.pid=:paperId and person.pid=:personId";
		Map<String, Long> parameter = new HashMap<String, Long>();
		parameter.put("paperId", paperId);
		parameter.put("personId", personId);
		return (ReviewPaper) super.selectSingle(hql, parameter, className + "-->selectByPaperIdAndPersonId(Long paperId, Long personId)");
	}

	public List<ReviewPaper> selectByPersonId(Long personId) {
		String hql = "from ReviewPaper where person.pid=:personId order by paper.paperNo desc";
		Map<String, Long> parameter = new HashMap<String, Long>();
		parameter.put("personId", personId);
		return (List<ReviewPaper>) super.ExecuteQuery(hql, parameter, className + "-->List<ReviewPaper> selectByPersonId(Long personId)");
	}

	public List<ReviewPaper> selectByJournalId(String isAccepted, Long journalId, Pagination pagination) {
		String hql = "from ReviewPaper where isAccepted=" + isAccepted + " and paper.person.journal.pid=" + journalId;
		return super.select(hql, null, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->List<ReviewPaper> selectByJournalId(String isAccepted,Long journalId,Pagination pagination)");
	}

	public Long selectCountByJournalIdAndIsAccepted(Long journalId, String isAccepted) {
		String hql = "select count(pid) from ReviewPaper where paper.person.journal.pid=:journalId and isAccepted=" + isAccepted;
		Map<String, Long> parameter = new HashMap<String, Long>();
		parameter.put("journalId", journalId);
		return (Long) super.selectSingle(hql, parameter, className + "-->selectCountByJournalId(Long journalId)");
	}

	public List<ReviewPaper> selectByPaperIdAndJournalId(Long paperId, Long journalId, Pagination pagination) {
		String hql = "from ReviewPaper where person.journal.pid=" + journalId + "and paper.pid=" + paperId;
		return super.select(hql, null, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->List<ReviewPaper> selectByPaperIdAndJournalId(Long paperId,Long journalId,Pagination pagination)");
	}

	public Long selectCountByJournalIdAndPaperId(Long journalId, Long paperId) {
		String hql = "select count(pid) from ReviewPaper where paper.person.journal.pid=:journalId and paper.pid=:paperId";
		Map<String, Long> parameter = new HashMap<String, Long>();
		parameter.put("journalId", journalId);
		parameter.put("paperId", paperId);
		return (Long) super.selectSingle(hql, parameter, className + "-->Long selectCountByJournalIdAndPaperId(Long journalId, Long paperId)");
	}

	public Long saveReviewPaper(ReviewPaper reviewPaper) {
		return super.saveObject(reviewPaper, className + "-->saveReviewPaper(ReviewPaper reviewPaper)");
	}

	public boolean deleteReviewPaper(ReviewPaper reviewPaper) {
		return super.deleteObject(new Object[] { reviewPaper.getPid() }, className + "-->deleteReviewPaper(ReviewPaper reviewPaper)");
	}
	public boolean deleteReviewPaperByPersonId(Long personId) {
		String hql = "delete from ReviewPaper where personId=:personId and score1=0";
		Map<String, Long> parameter = new HashMap<String, Long>();
		parameter.put("personId", personId);
		return super.ExecuteNonQuery(hql,parameter, className + "-->deleteReviewPaperByPersonId(Long personId)");
	}
	public Long selectCountReviewPaperByIsAccepted(Long personId, String isAccepted) {
		String hql = null;
		if ("0".equals(isAccepted)) {
			hql = "select count(pid) from ReviewPaper where person.pid = " + personId + " and isAccepted = " + isAccepted;
		} else {
			hql = "select count(pid) from ReviewPaper where person.pid = " + personId + "and isAccepted != " + "0";
		}
		return (Long) super.selectSingle(hql, null, className + "-->selectCountReviewPaperByIsAccepted(Long personId,String isAccepted))");
	}

	public List<ReviewPaper> selectByPaperId(Long paperId) {
		String hql = "from ReviewPaper where paper.pid=:paperId";
		Map<String, Long> parameter = new HashMap<String, Long>();
		parameter.put("paperId", paperId);
		return (List<ReviewPaper>) super.ExecuteQuery(hql, parameter, className + "-->selectByPaperId(Long paperId)");
	}
}
